package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.user.impl.dao.UserProfileDao;
import com.jiantong.user.model.UserProfile;
import com.jiantong.user.service.UserProfileWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;


/**
 * Created by ThinkPad on 2017/8/3.
 */
@Service
@Slf4j
@RpcProvider
public class UserProfileWriteServiceImpl implements UserProfileWriteService {

    private UserProfileDao userProfileDao;
    @Autowired
    public UserProfileWriteServiceImpl(UserProfileDao userProfileDao) {
        this.userProfileDao = userProfileDao;
    }

    @Override
    public Response<Long> create(UserProfile userProfile) {
        try {
            userProfileDao.create(userProfile);
            return Response.ok(userProfile.getId());
        } catch (DuplicateKeyException e) {
            log.error("failed to create {}, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("user.loginId.duplicate");
        } catch (Exception e) {
            log.error("failed to create {}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("user.create.fail");
        }
    }
}
